package team.star.werespos.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.star.werespos.model.entity.Table;
import team.star.werespos.model.vo.ResultVO;
import team.star.werespos.service.TableService;
import team.star.werespos.util.Result;

import java.util.List;

/**
 * TableController class<br/>
 *
 * @author hdonghong
 * @since 2019/06/06
 */
@Slf4j
@RequestMapping("/tables")
@RestController
public class TableController {

    @Autowired
    private TableService tableService;

    @PostMapping("/new")
    public ResultVO<?> newTable(@RequestBody Table table) {
        tableService.insertOne(table);
        return Result.success(table);
    }


    /**
     * 获取餐桌列表
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<Table>> lisTables(){
        return Result.success(
                tableService.listTables()
        );
    }

    @PostMapping("/{tableId}/update")
    public ResultVO<?> updateById(@PathVariable Integer tableId,
                                  @RequestBody Table table) {
        table.setTableId(tableId);
        tableService.updateById(table);
        return Result.success();
    }

    @GetMapping("/{tableId}/delete")
    public ResultVO<?> deleteById(@PathVariable Integer tableId) {
        tableService.deleteById(
                new Table().setTableId(tableId)
        );
        return Result.success();
    }
}
